package com.yj.suncity.activity;

import org.json.JSONObject;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.yj.suncity.R;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.constant.ShareCon;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.setting.AboutUsActivity;
import com.yj.suncity.setting.FeedbackActivity;
import com.yj.suncity.setting.HelpActivity;
import com.yj.suncity.user.UpdateAPKServer;
import com.yj.suncity.user.bean.VersionBean;
import com.yj.suncity.utis.DialogUtils;
import com.yj.suncity.utis.DialogUtils.ReshActivity;
import com.yj.suncity.utis.SharedPreferencesUtil;
import com.yj.suncity.utis.ToastUtil;
import com.yj.suncity.utis.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class SettingActivity extends Activity {
	private Context context;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private boolean isVoice;
	private ImageView mImag_on, mImag_off;
	private Button mBtn_outlogin;
	private SharedPreferencesUtil preferencesUtil = SharedPreferencesUtil
			.getInstance();
	private boolean isLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		context = this;

		findView();
	}

	private void findView() {
		mImag_on = (ImageView) findViewById(R.id.img_on);
		mImag_off = (ImageView) findViewById(R.id.img_off);
		mBtn_outlogin = (Button) findViewById(R.id.btn_outlogin);
	}

	@Override
	protected void onResume() {
		isLogin = preferencesUtil.getBoolean(context, ShareCon.USERFILE,
				ShareCon.ISLOGIN, false);
		isVoice = preferencesUtil.getBoolean(context, ShareCon.USERFILE,
				ShareCon.SOUND, true);
		if (isLogin) {
			mBtn_outlogin.setVisibility(View.VISIBLE);
		} else {
			mBtn_outlogin.setVisibility(View.GONE);
		}
		super.onResume();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_outlogin:
			preferencesUtil.putBoolean(context, ShareCon.USERFILE,
					ShareCon.ISLOGIN, false);
			preferencesUtil.putBoolean(context, ShareCon.USERFILE,
					ShareCon.USERTYPE, false);
			mBtn_outlogin.setVisibility(View.GONE);
			break;
		case R.id.rela_voice:
			// 声音
			if (isVoice) {
				isVoice = false;
				mImag_on.setVisibility(View.INVISIBLE);
				mImag_off.setVisibility(View.VISIBLE);
			} else {
				isVoice = true;
				mImag_on.setVisibility(View.VISIBLE);
				mImag_off.setVisibility(View.INVISIBLE);
			}
			preferencesUtil.putBoolean(context, ShareCon.USERFILE,
					ShareCon.SOUND, isVoice);
			break;
		case R.id.rela_clean:
			// 清空缓存
			imageLoader.clearDiscCache();
			imageLoader.clearMemoryCache();
			ToastUtil.ShowToast(context, "清理完成！");
			break;
		case R.id.rela_feedback:
			// 反馈
			startActivity(new Intent(this, FeedbackActivity.class));
			break;
		case R.id.rela_evaluate:
			// 评价
			break;
		case R.id.rela_help:
			// 帮助
			startActivity(new Intent(this, HelpActivity.class));
			break;
		case R.id.rela_about:
			// 关于
			startActivity(new Intent(this, AboutUsActivity.class));
			break;
		case R.id.version_new:
			// 版本更新
			String currentVersion = getPresentVersion();
			Version(currentVersion);
			break;
		}
	}

	private String getPresentVersion() {
		try {
			PackageManager packageManager = getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					getPackageName(), 0);
			return packageInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	private void Version(final String currentVersion) {
		String path = URLCon.版本更新;
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null, null,
				false, null, false, "检查新版本......", new ResultCallBack() {
					@Override
					public void JSONResult(JSONObject jsonObject) {
						final VersionBean versionBean = new VersionBean(jsonObject);
						if (versionBean.getSuccess().equals("1")) {
							if (!versionBean.getVersion_number().equals(
									currentVersion)) {
								DialogUtils dialogUtils = new DialogUtils();
								dialogUtils.backDialog(context, "有新版本，立即更新？",
										true, new ReshActivity() {
											@Override
											public void reshActivity() {
												;
												Intent updateIntent = new Intent(
														context, UpdateAPKServer.class);
												updateIntent.putExtra("titleId", getResources()
														.getString(R.string.app_name));
												updateIntent.putExtra("url",versionBean.getApp_url());
												//updateIntent.putExtra("filesize", filesize);
												startService(updateIntent);
											}
										});
							} else {
								DialogUtils dialogUtils = new DialogUtils();
								dialogUtils.backDialog(context, "暂无版本更新！",
										false, null);
							}
						} else {
							Utils.showToast(context, versionBean.getMsg());
						}
					}
				});
		asyncTaskUtil.execute(path);
	}
}
